package com.xframe.xdal.core.util.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.xframe.xdal.core.util.XReflect;
import lombok.Getter;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.data.general.DefaultPieDataset;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;


public class PieChartOpt {



    private DefaultPieDataset pieDataset = new DefaultPieDataset();
    /**
     * 宿主对象
     */
    @Getter
    private PdfBuilder pdfBuilder;

    private Collection<Object> values;

    private String keyName;
    private String valueName;

    private int width = 800;
    private int height = 500;

    private boolean percentageFlag = true;

    /**
     * 没有数据时的消息
     */
    private String noDataMessage = "暂无数据";

    public static PieChartOpt buildPieChartOpt(PdfBuilder pdfBuilder,Collection<Object> values,boolean percentageFlag
            ,String keyName,String valueName
    ){
        PieChartOpt pieChartOpt = new PieChartOpt();
        pieChartOpt.pdfBuilder = pdfBuilder;
        pieChartOpt.keyName = keyName;
        pieChartOpt.valueName = valueName;
        pieChartOpt.values = values;
        pieChartOpt.percentageFlag = percentageFlag;
        return pieChartOpt;
    }

    public static PieChartOpt buildPieChartOpt(Collection<Object> values,boolean percentageFlag
            ,String keyName,String valueName
    ){
        PieChartOpt pieChartOpt = new PieChartOpt();
        pieChartOpt.keyName = keyName;
        pieChartOpt.valueName = valueName;
        pieChartOpt.values = values;
        pieChartOpt.percentageFlag = percentageFlag;
        return pieChartOpt;
    }

    /**
     * 生成饼图
     *
     * @return 返回生成的byte数组
     */
    public void buildPieChart() throws DocumentException, IOException {
        for (Object entry : values) {
            String keyValue = (String) XReflect.getFieldValue(entry, this.keyName);
            Number value = (Number) XReflect.getFieldValue(entry, this.valueName);
            pieDataset.setValue(keyValue, value);
        }
        /**
         * 生成一个饼图的图表
         * 分别是:显示图表的标题、需要提供对应图表的DateSet对象、是否显示图例、是否生成贴士以及是否生成URL链接
         */
        JFreeChart chart = ChartFactory.createPieChart("", pieDataset, true, true, true);
        setPieRender(chart.getPlot());
        ByteArrayOutputStream bas = new ByteArrayOutputStream();
        ChartUtils.writeChartAsJPEG(bas, 1.0f, chart, width, height, null);
        com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(bas.toByteArray());
        image.setAlignment(com.itextpdf.text.Image.ALIGN_CENTER);
        image.scaleAbsolute(width, height);
        pdfBuilder.getDocument().add(image);
    }


    /**
     * 生成饼图
     *
     * @return 返回生成的byte数组
     */
    public byte[] buildPieChartImageByteArray() throws IOException {
        for (Object entry : values) {
            String keyValue = (String) XReflect.getFieldValue(entry, this.keyName);
            Number value = (Number) XReflect.getFieldValue(entry, this.valueName);
            pieDataset.setValue(keyValue, value);
        }
        /**
         * 生成一个饼图的图表
         * 分别是:显示图表的标题、需要提供对应图表的DateSet对象、是否显示图例、是否生成贴士以及是否生成URL链接
         */
        JFreeChart chart = ChartFactory.createPieChart("", pieDataset, true, true, true);
        setPieRender(chart.getPlot());
        ByteArrayOutputStream bas = new ByteArrayOutputStream();
        ChartUtils.writeChartAsJPEG(bas, 1.0f, chart, width, height, null);
        return bas.toByteArray();
    }

    /**
     * 设置饼状图渲染
     *
     * @param plot 饼图对象
     */
    private void setPieRender(Plot plot) {

        plot.setNoDataMessage(noDataMessage);
        plot.setInsets(new RectangleInsets(10, 10, 5, 10));
        PiePlot piePlot = (PiePlot) plot;
        piePlot.setInsets(new RectangleInsets(0, 0, 0, 0));
        // 圆形
        piePlot.setCircular(true);
        piePlot.setLabelGap(0.01);
        piePlot.setInteriorGap(0.05D);
        // 图例形状
        piePlot.setLegendItemShape(new Rectangle(10, 10));
        piePlot.setIgnoreNullValues(true);
        // 去掉背景色
        piePlot.setLabelBackgroundPaint(null);
        // 去掉阴影
        piePlot.setLabelShadowPaint(null);
        // 去掉边框
        piePlot.setLabelOutlinePaint(null);
        piePlot.setShadowPaint(null);
        // 0:category 1:value:2 :percentage
        // 显示标签数据 {0} 代表 饼状图分类的名字,{1} 显示具体数值,{2} 显示百分比
        //{0}:{1} 英雄联盟:20 {0}:{2} 英雄联盟 20%
        if(percentageFlag) {
            piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{2}"));
        }else {
            piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}"));
        }

    }
}
